package org.jeecg.modules.demo.mytest.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.ReadPageNumUtils;
import org.jeecg.modules.demo.mytest.entity.MtProjectDocument;
import org.jeecg.modules.demo.mytest.mapper.MtProjectDocumentMapper;
import org.jeecg.modules.demo.mytest.service.IMtProjectDocumentService;
import org.jeecg.modules.demo.mytest.service.IMtProjectService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 项目文档表
 * @Author: thankgod7
 * @Date: 2023-07-28
 * @Version: V1.0
 */
@Service
public class MtProjectDocumentServiceImpl extends ServiceImpl<MtProjectDocumentMapper, MtProjectDocument> implements IMtProjectDocumentService {

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    @Resource
    private IMtProjectService mtProjectService;

    @Resource
    private MtProjectDocumentMapper mtProjectDocumentMapper;

    @Override
    public List<MtProjectDocument> selectByMainId(String mainId) {
        return mtProjectDocumentMapper.selectByMainId(mainId);
    }

    @Override
    public void reCount(String dataId) {
        MtProjectDocument document = this.getById(dataId);
        if (Objects.nonNull(document) && StringUtils.isNotBlank(document.getFilelist())) {
            String[] filePaths = document.getFilelist().split(",");
            document.setCopies(filePaths.length);
            int pages = 0;
            for (String path : filePaths) {
                File file = new File(uploadpath + File.separator + path);
                if (file.exists()) {
                    Integer pageNum = ReadPageNumUtils.read(uploadpath + File.separator + path);
                    pages = pages + pageNum;
                }
            }
            document.setPages(pages);
            this.updateById(document);
        }
    }
}
